/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.webos.java.explorer.ftypes;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import org.json.JSONObject;
import org.webos.java.base.Frame;
import org.webos.java.base.FrameType;
import org.webos.java.base.RemoteMessage;
import org.webos.java.base.fs.FileDescriptor;
import org.webos.java.explorer.Explorer;
import org.webos.java.root.Log;
import org.webos.java.base.NetworkManager;
import org.webos.java.base.fs.FileManager;
/**
 *
 * @author kostya
 */

@FrameType(code="text-editor", jsClass="explorer.TextEditor")
public class TextEditor extends Frame{

    private FileDescriptor fileDescriptor = null;
    private JSONObject data = new JSONObject();
    private Explorer explorer = null;

    public TextEditor(Explorer explorer, FileDescriptor aDescriptor) {
        super(explorer, null);
        this.explorer = explorer;
        fileDescriptor = aDescriptor;
        try {
            if(fileDescriptor.getId()!=null){
                data.put("name", fileDescriptor.getName());
                setTitle("Text editor: "+fileDescriptor.getName());
                //Read document to string buffer
                StringBuffer buffer = new StringBuffer();
                BufferedReader reader =
                        new BufferedReader(new InputStreamReader(fileDescriptor.getProvider().getInputStream(fileDescriptor), "utf-8"));
                String d = null;
                while((d = reader.readLine())!=null){
                    buffer.append(d+"\n");
                }
                data.put("text", buffer.toString());
                reader.close();
            }else{
//                data.put("name", "New file");
                setTitle("Text editor: new document");
            }
            setIcon("explorer_icn explorer_icn_text_file");
        } catch (Throwable t) {
            Log.error(this, "Exception thrown: " + t.getMessage());
            t.printStackTrace();
        }
    }

    @RemoteMessage
    public JSONObject getData(JSONObject data){
        return this.data;
    }

    @RemoteMessage
    public void updateData(JSONObject data){
        this.data = data;
    }

    @RemoteMessage
    public JSONObject saveTextFile(JSONObject data){
        this.data = data;
        try {
            boolean newFile = fileDescriptor.getId()==null;
            fileDescriptor.setName(data.optString("name"));
            FileManager.checkFileDescriptorName(fileDescriptor);
            OutputStream stream = fileDescriptor.getProvider().getOutputStream(fileDescriptor);
            OutputStreamWriter writer = new OutputStreamWriter(stream, "utf-8");
            writer.write(data.optString("text", ""));
            writer.close();
            Log.debug(this, "ID of new file: "+fileDescriptor.getId());
            explorer.fileUpdated(fileDescriptor, newFile);
            setTitle("Text editor: "+fileDescriptor.getName());
        } catch (Throwable t) {
            Log.error(this, "Exception thrown: " + t.getMessage());
            t.printStackTrace();
            return NetworkManager.getErrorObject(2, "Can't save file");
        }
        return null;
    }
}
